<?php 

class Admin_View_Helper_Order extends Zend_View_Helper_Abstract{
	
	public $data;
	/**
	 * Create this class similar to 'buyback/view/helper/cart.php'
	 * Create the required forms by adding them dynamically at run time,
	 */
	
	public function Order(){
		return $this;
	}
	
	public function displayOrder($orderId, Admin_Form_OrderAdjustment $orderForm, Admin_Form_ItemAdjustment $itemForm, Admin_Form_ItemReject $rejectForm){
		$html = '';
		/* Order Details */
		try{
			Doctrine_Model_Orders::adjustTotal($orderId);
			$order = Doctrine_Model_Orders::getOrderDetails($orderId);
			$orderItems = Doctrine_Model_OrderItems::getOrderItems($orderId);
			$html = "<div class='orderWrapper'>";
			$html .= "<div class='data-cell-thin'>Order Id</div>";
			$html .= "<div class='data-cell-mid'>Total</div>";
			$html .= "<div class='data-cell-mid'>Adj Total</div>";
			$html .= "<div class='data-cell-mid'>User Id</div>";
			$html .= "<div class='data-cell-mid'>has Status</div>";
			$html .="<div class='data-cell-mid'>Status Date</div>";
			$html .="<div class='clear'></div>";
			$html .="</div>";
			
			$html .="<div class='data-cell-thin'>" . $order['id'] . "</a></div>";
			$html .="<div class='data-cell-mid'>" . Custom_FormatCurrency::formatAmount($order['total']/100) . "</div>";
			$html .="<div class='data-cell-mid'>" . Custom_FormatCurrency::formatAmount($order['adjustedTotal']/100)  . "</div>";
			$html .="<div class='data-cell-mid'>" . $order['userId'] . "</div>";
			$html .="<div class='data-cell-mid'>" . $order['status'] . "</div>";
			$html .="<div class='data-cell-mid'>";
				 
			switch($order['status']){
				case 'created':
					$html .= $order['createdOn'];
					break;
				case 'shipped':
					$html .= $order['shippedOn'];
					break;
				case 'received':
					$html .= $order['receivedOn'];
					break;
				case 'under_review':
					$html .= $order['checkedOn'];
					break;
				case 'pending':
					$html .= $order['checkedOn'];
					break;
				case 'rejected':
					$html .= $order['completedOn'];
					break;
				case 'paid':
					$html .= $order['completedOn'];
					break;
				case 'closed':
					$html .= $order['closedOn'];
					break;
				}
			$html .= "</div>";
			$html .= "<div class='clear'></div>";
			$html .= "<div class='data-cell-mid'>Value</div>";
			$html .= "<div class='data-cell-mid'>Comment</div>";
			$html .= "<div class='data-cell-wide'>Note</div>";
			$html .="<div class='data-cell-mid'>created</div>";
			$html .="<div class='data-cell-mid'>updated</div>";
			$html .="<div class='clear'></div>";
			if($order['OrderAdjustments']):
				foreach($order['OrderAdjustments'] as $orderAdjust):
					$html .= "<div class='data-cell-mid'>" . Custom_FormatCurrency::formatAmount($orderAdjust['adjustmentValue']/100) . "</div>";
					$html .= "<div class='data-cell-mid'>" . $orderAdjust['adjustmentComment'] . "</div>";
					$html .= "<div class='data-cell-wide'>" . $orderAdjust['adjustmentNote'] . "</div>";
					$html .="<div class='data-cell-mid'>" . $orderAdjust['createdOn'] . "</div>";
					$html .="<div class='data-cell-mid'>" . $orderAdjust['updatedOn'] . "</div>";
					$html .="<div class='clear'></div>";
				endforeach;
			endif;
		
			$orderForm->getElement('orderId')->setValue($orderId);
			$html .= $this->OrderAdjustment($orderForm);
			
			$i = 0;
			foreach($orderItems as $item):
			$itemType = $item['itemType'] . "s";
		 
			$class = ($i%2 ==0 )?"even":"odd";
			$i++;
			$status = Doctrine_Model_OrderItems::getItemStatus($item['id']);
			$condition = Doctrine_Model_OrderItems::getItemCondition($item['id']);
			if($status == 'not_checked_in'){
				$form = new Admin_Form_CheckIn();
			}else{
				$form = new Admin_Form_Reset();
			}
			$form->getElement('orderId')->setValue($orderId);
			$form->getElement('orderItemsId')->setValue($item['id']);
			$conForm = new Admin_Form_Condition();
			$conForm->getElement('orderId')->setValue($orderId);
			$conForm->getElement('orderItemsId')->setValue($item['id']);
			$conForm->getElement('itemCondition')->setLabel('Condition : ' . $condition);
			$html .= "<div class= $class>";
			$html .= "<div class='data-cell-mid'>" . Doctrine_Model_OrderItems::getItemStatus($item['id']) . "" . $form->render() . "</div>"; 
			$html .= "<div class='data-cell-thin'>" . $item['priceSource'] . "<br/>" . $item['priceMetrics'] . "</div>";
			$html .= "<div class='data-cell-mid'><img src=" . $item[$itemType]['image'] . "></img></div>";
			$html .= "<div class='data-cell-wide'>ISBN13: " . $item[$itemType]['Isbn13'] . "<br/>ISBN10: " . $item[$itemType]['Isbn10'] . "<br/>Title: ";
			
			$title = $item[$itemType]['title']; 
			if(strlen($title) > 50){
				$title = substr($title, 0, 47);
				$title .= "...";
			}
			$html .= $title . "<br/>Author/s: ";
			$author = $item[$itemType]['author']; 
			if(strlen($author)> 50){
				$author = substr($author, 0, 47);
				$author .= "...";
			}
			$html .= $author . "<br/>";
			$html .= $conForm->render();
			$html .= "</div>";
			$html .= "<div class='data-cell-mid'>" . Custom_FormatCurrency::formatAmount($item['price']/100) . "</div>";
			$html .= "<div class='data-cell-wide'>";
			$itemForm->getElement('orderId')->setValue($orderId);
			$itemForm->getElement('orderItemsId')->setValue($item['id']);
			$html .= $this->ItemAdjustment($itemForm);
			$html .= "</div>";
			$html .= "<div class='data-cell-wide'>";
			$rejectForm->getElement('orderId')->setValue($orderId);
			$rejectForm->getElement('orderItemsId')->setValue($item['id']);
			$html .= $this->ItemRejection($rejectForm);
			$html .= "</div>";
			$html .= "</div>";
			$html .= "<div class='clear'></div>";
				if($item['ItemAdjustments']):
					$html .= "<div class='data-cell-mid'>Value</div>";
					$html .= "<div class='data-cell-mid'>Reason</div>";
					$html .= "<div class='data-cell-wide'>Note</div>";
					$html .="<div class='data-cell-mid'>created</div>";
					$html .="<div class='data-cell-mid'>updated</div>";
					$html .="<div class='clear'></div>";
				endif;
				foreach($item['ItemAdjustments'] as $itemAdjust):
					$html .= "<div class='data-cell-mid'>" . Custom_FormatCurrency::formatAmount($itemAdjust['adjustmentValue']/100) . "</div>";
					$html .= "<div class='data-cell-mid'>" . $itemAdjust['adjustmentReason'] . "</div>";
					$html .= "<div class='data-cell-wide'>" . $itemAdjust['adjustmentNote'] . "</div>";
					$html .="<div class='data-cell-mid'>" . $itemAdjust['createdOn'] . "</div>";
					$html .="<div class='data-cell-mid'>" . $itemAdjust['updatedOn'] . "</div>";
					$html .="<div class='clear'></div>";
				endforeach;
			endforeach;
			
			return $html;
	
		}catch(Exception $e){
			switch($e->getMessage()){
				case 1:
					return "Permission Denied";
					break;
				case 2:
					return "Order Does Not Exist";
					break;
			}
			
		}
	
	}
	public function OrderAdjustment(Admin_Form_OrderAdjustment $form){
		$html = $form->render();
		return $html;
	}
 
	public function ItemAdjustment(Admin_Form_ItemAdjustment $form){
		$html = $form->render();
		return $html;
	}
	
	public function ItemRejection(Admin_Form_ItemReject $form){
		$html = $form->render();
		return $html;
	}	
}